package cn.yy.cloudpay.view;

import cn.yy.cloudpay.R;
import cn.yy.cloudpay.listener.ICustomDialogListener;
import cn.yy.cloudpay.util.Constant;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class CustomDialog extends Dialog {

	private static Integer checkedType = Constant.PAY_TYPE_MICROPAY;
	
	public CustomDialog(Context context) {
		super(context);
	}

	public CustomDialog(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private Context context;
		private LayoutInflater inflater;
		private ICustomDialogListener listener;
		private RadioGroup radioGroup;

		public Builder(Context context) {
			checkedType = Constant.PAY_TYPE_MICROPAY;
			this.context = context;
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public CustomDialog create() {
			// instantiate the dialog with the custom Theme
			final CustomDialog dialog = new CustomDialog(context,R.style.Dialog);
			View layout = inflater.inflate(R.layout.layout_custom_dialog, null);
			radioGroup = (RadioGroup) layout.findViewById(R.id.rg_customdialog_paytype);
			radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(RadioGroup group, int checkedId) {
					switch(group.getCheckedRadioButtonId()) {
					case R.id.rb_customdialog_scancode:
						checkedType = Constant.PAY_TYPE_MICROPAY;
						break;
					case R.id.rb_customdialog_pos:
						checkedType = Constant.PAY_TYPE_ALIPAY;
						break;
					case R.id.rb_customdialog_others:
						checkedType = Constant.PAY_TYPE_BANKCARD;
						break;
					default:
						break;
					}
				}
			});
			Button btnConfirm = (Button) layout.findViewById(R.id.btn_layout_customdialog_positive);
			btnConfirm.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					listener.onPositiveClickListener(dialog,v,checkedType);
				}
			});
			Button btnCancel = (Button) layout.findViewById(R.id.btn_layout_customdialog_negative);
			btnCancel.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					listener.onNegativeClickListener(dialog);
				}
			});
			dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			//点击屏幕对话框不会取消
			dialog.setCanceledOnTouchOutside(false);
			return dialog;
		}
		
		public void setOnDialogClickListener(ICustomDialogListener listener) {
			this.listener = listener;
		}
	}
}